package cn.cdeden.knowledge.controller.schedule;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.knowledge.domain.SysResourceType;
import cn.cdeden.knowledge.service.SysResourceTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * *作者：gjx
 * *日期：2024/7/9    16:16
 * *文件描述：
 */
@RestController
//@Api("资源类型管理")
@RequestMapping("/api/sysResourceType")
public class SysResourceTypeController {

    @Autowired
    private SysResourceTypeService sysResourceTypeService;


    @GetMapping("/get-all")
//    @Log("查询全部的资源类型")
//    @ApiOperation("查询全部的资源类型")
    public R<List<SysResourceType>> getAll() {
        return R.ok(sysResourceTypeService.list());
    }

    @PostMapping("/add")
//    @Log("添加资源类型")
//    @ApiOperation("添加资源类型")
    public R add(@RequestBody SysResourceType sysResourceType) {
        List<SysResourceType> sysResourceTypeList = sysResourceTypeService.list(new QueryWrapper<SysResourceType>().eq("resource_type_name", sysResourceType.getResourceTypeName()));
        if (!sysResourceTypeList.isEmpty()) {
            return R.fail("406", "该资源类型已经存在！");
        }
        return R.ok(sysResourceTypeService.save(sysResourceType));
    }

    @GetMapping("/edit/{id}")
//    @Log("编辑资源类型回显")
//    @ApiOperation("编辑资源类型回显")
    public R<SysResourceType> edit(@PathVariable("id") Integer resourceTypeId) {
        return R.ok(sysResourceTypeService.getById(resourceTypeId));
    }

    @PutMapping("/edit")
//    @Log("编辑资源类型")
//    @ApiOperation("编辑资源类型")
    public R update(@RequestBody SysResourceType sysResourceType) {
        List<SysResourceType> sysResourceTypeList = sysResourceTypeService.list(new QueryWrapper<SysResourceType>()
                .eq("resource_type_name", sysResourceType.getResourceTypeName())
        .ne("resource_id", sysResourceType.getResourceId()));
        if (!sysResourceTypeList.isEmpty()) {
            return R.fail("406","该资源类型已经存在！");
        }
        return R.ok(sysResourceTypeService.updateById(sysResourceType));
    }

    @DeleteMapping("/delete/{id}")
//    @Log("删除资源类型")
//    @ApiOperation("删除资源类型")
    public R delete(@PathVariable("id") Integer resourceTypeId) {
        return R.ok(sysResourceTypeService.removeById(resourceTypeId));
    }
}
